//
//  WalletUnbindVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/27.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "WalletUnbindVC.h"

@interface WalletUnbindVC () {
    int count;
}

@property(weak, nonatomic) IBOutlet UIButton *backBt;

@property(weak, nonatomic) IBOutlet UIView *smscodeView;
@property(weak, nonatomic) IBOutlet UITextField *smscodeTf;
@property(weak, nonatomic) IBOutlet UIButton *codeBt;
@property(weak, nonatomic) IBOutlet UIButton *operateBt;

@end

@implementation WalletUnbindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    self.smscodeView.layer.cornerRadius = globalRadius;
    self.codeBt.layer.cornerRadius = globalRadius;
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [[self.codeBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.smscodeTf resignFirstResponder];
        [self requestSms];
    }];
    [[self.operateBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.smscodeTf resignFirstResponder];
        NSString *smscode = self.smscodeTf.text;
        if (smscode<=0) {
            [JPToast showText:@"请输入短信验证码"];
            return;
        }
        [self requestUnBind];
    }];
}

- (void)requestSms {
    [self showLoading:@"正在获取验证码"];
    [[[HttpRequest shareInstance] signGet:w_unbinding_sms map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            self->count = 60;
            [self countDown];
            [JPToast showText:x[@"message"]];
        } else {
            NSString *msg = x[@"message"];
            msg = [msg containsString:@"流控Permits"] ? @"您的验证操作过于频繁，请稍后再试" : msg;
            [JPToast showText:msg];
            [self.codeBt setEnabled:YES];
        }
        [self hideLoading];
    } error:^(NSError *error) {
        [self.codeBt setEnabled:YES];
        [JPToast showText:@"网络错误"];
        [self hideLoading];
    }];
}

- (void)requestUnBind {
    NSDictionary *dic = @{@"smsCode":self.smscodeTf.text};
    [[[HttpRequest shareInstance] signPost:w_unbinding map:[NSMutableDictionary dictionaryWithDictionary:dic] ] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            [JPToast showText:@"解绑成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [JPToast showText:@"网络错误"];
    }];
}

- (void)countDown {
    if (count<=0) {
        [self.codeBt setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.codeBt setEnabled:YES];
    } else {
        [self.codeBt setTitle:[NSString stringWithFormat:@"%dS",count] forState:UIControlStateNormal];
        [self.codeBt setEnabled:NO];
        count--;
        [self performSelector:@selector(countDown) withObject:nil afterDelay:1];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
